<?php
class ccdsObjectConfig
{
    private $serverdir;
    private $classes;
    private $extensions;
    private $ccdSQL;

    function configure( $serverdir )
    {
        if ( $serverdir != "" )
        {
            $this->serverdir=$serverdir;
            $this->classes=array();
        }
        else
        {
            return false;
        }
        $this->ccdSQL=ccdSQL::getInstance();
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */
    private function __loadClass( $class_id )
    {

        $query="SELECT * FROM classes 
							LEFT JOIN extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							WHERE class_id='" . $class_id . "'
							LIMIT 1
		";
        $res=$this->ccdSQL->query( $query );
        $class=$this->ccdSQL->fetch_assoc( $res );

        $path_array=array();
        $config_source=
            $this->serverdir . $class['project_dir'] . "/" . $class['ext_dir'] . "/" . $class['class_dir'] . "/config/"
            . $class['class_name'] . ".conf";

        $xml=new ccdsXMLparser( $config_source );

        $this->classes[$class['class_id']]=$xml->getArray();
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getClass( $class_id )
    {
        if ( !is_numeric( $class_id ) )
        {
            trigger_error( "ccdsObjectConfig: \$class_id is not an int" );
            return false;
        }

        if ( !isset( $this->classes[$class_id] ) )
        {
            $this->__loadClass( $class_id );
        }
        return $this->classes[$class_id];
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function saveClass( $class_id, $config )
    {
        $query="SELECT * FROM classes 
							LEFT JOIN extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							
							WHERE class_id='" . $class_id . "'
							LIMIT 1
		";
        $res=$this->ccdSQL->query( $query );
        $class=$this->ccdSQL->fetch_assoc( $res );

        $old_config=$this->getClass( $class_id );

        if ( !is_array( $old_config ) )
            $old_config=array();
        $config=array_merge( $old_config, $config );

        $path_array=array();
        $config_source=
            $this->serverdir . $class['project_dir'] . "/" . $class['ext_dir'] . "/" . $class['class_dir'] . "/config/"
            . $class['class_name'] . ".conf";
        $config_source_array=
            $this->serverdir . $class['project_dir'] . "/" . $class['ext_dir'] . "/" . $class['class_dir'] . "/config/"
            . $class['class_name'] . "2.array";
        $config['project']=$class['project_id'];
        $xml_generator=new ccdsXMLgenerator();
        $xml=$xml_generator->generateXML( $config, $class['class_name'] );
        file_put_contents( $config_source, $xml );

        $config=ccdsConfig::getInstance();
        chmod( $config_source, $config->get( 'FILE_CHMOD' ) );

        unset( $this->classes[$class['class_id']] );
        unset( $xml );
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    private function __loadExtension( $ext_id )
    {

        $query="SELECT extensions ON class_key_ext_id=ext_id 
							LEFT JOIN projects ON ext_key_project_id=project_id
							WHERE class_id='" . $ext_id . "'
							LIMIT 1
		";
        $res=$this->ccdSQL->query( $query );
        $ext=$this->ccdSQL->fetch_assoc( $res );

        $path_array=array();
        $config_source=
            $this->serverdir . $ext['project_dir'] . "/" . $ext['ext_dir'] . "/" . $ext['class_name'] . ".conf";
        $xml=new ccdsXMLparser( $config_source );

        $this->extensions[$ext['ext_id']]=$xml->getArray();
        unset( $xml );
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getExtension( $ext_id )
    {
        if ( !is_numeric( $ext_id ) )
            trigger_error( "ccdsObjectConfig: \$class_id is not an int" );

        if ( !is_array( $this->extensions[$ext_id] ) )
        {
            $this->__loadExtension( $ext_id );
        }
        return $this->extensions[$ext_id];
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

    function __triggerError( $error_msg )
    {
        $this->errors[]=$error_msg;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function error( )
    {
        if ( count( $this->errors ) > 0 )
        {
            $errormsg=implode( "<br><br>", $this->errors );
            $this->errors=array();
            return $errormsg;
        }
        else
        {
            return false;
        }
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    static function &getInstance( )
    {
        static $obj;

        if ( !$obj )
        {
            $config=ccdsConfig::getInstance();
            $obj=new ccdsObjectConfig();
            $obj->configure( $config->get( 'DOC_ROOT' ) . $config->get( 'WEB_ROOT' ) . "ccds_extensions/" );
        }
        return $obj;
    }
}
?>